package com.smart.campus.web.controller.system;

import java.util.List;
import java.util.Map;

import com.smart.campus.common.config.Global;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.AppVersion;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.service.IAppVersionService;
import com.smart.campus.school.service.IMsgService;
import com.smart.campus.school.service.IStudentService;
import com.smart.campus.system.domain.SysDept;
import com.smart.campus.system.domain.SysNotice;
import com.smart.campus.system.service.ISysDeptService;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import com.smart.campus.common.config.Global;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.system.domain.SysMenu;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.system.service.ISysConfigService;
import com.smart.campus.system.service.ISysMenuService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 首页 业务处理
 * 
 * @author 智慧校园项目组
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IMsgService msgService;
    @Autowired
    private IAppVersionService appVersionService;
    @Autowired
    private IStudentService studentService;
    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);

        Long schoolId = SchoolUtil.getSchoolId();
        if(schoolId != null){
            SysDept sysDept = deptService.selectDeptById(schoolId);
            mmap.put("schoolName", sysDept.getDeptName());
        }else {
            mmap.put("schoolName", "智慧校园平台");
        }
        //加载消息中心
        Msg msg = new Msg();
        msg.setUserId(user.getUserId());
        msg.setUserType(1);
        msg.setStatus(0);
        List<Msg> msgList = msgService.selectMsgList(msg);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("demoEnabled", Global.isDemoEnabled());
        mmap.put("msgCount", msgList.size());
        mmap.put("env", Global.getEnvName());



        AppVersion appVersion = appVersionService.getLastVersion();
        mmap.put("appUrl", appVersion == null ? "" : appVersion.getUrl());
        return "index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap)
    {
        return "skin";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
        mmap.put("version", Global.getVersion());
        Map<String,Integer> countMap = studentService.getTotalCount();
        mmap.put("studentCount", countMap.get("studentCount"));
        mmap.put("teacherCount", Integer.valueOf(String.valueOf(countMap.get("teacherCount")))-1);
        mmap.put("classCount",countMap.get("classCount"));
        mmap.put("gradeCount", countMap.get("gradeCount"));
        return "main";
    }

    // 消息中心
    @GetMapping("/msg")
    public String msg(ModelMap mmap)
    {
        return "school/msg/msg";
    }

    /**
     * 查询消息中心列表
     */
    @PostMapping("/msg/list")
    @ResponseBody
    public TableDataInfo list()
    {
        startPage();
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        //加载消息中心
        Msg msg = new Msg();
        msg.setUserId(user.getUserId());
        msg.setUserType(1);
        List<Msg> msgList = msgService.selectMsgList(msg);
        return getDataTable(msgList);
    }
}
